<?php 
/*
   Copyright 2010, Sanket Parab

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
   This is part of the e-TAT Architecture Package and will be featured in the final e-TAT Application (Phase 1).
   
   Survey class: Holds data on each survey created by the instructor
 *
 */
class Survey {
	/**
	 * Constructor
	 * @param $id ID of the survey
	 */
	function __construct($id) {
		$query = mysql_query("SELECT * FROM `survey` WHERE `id` = '$id'") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			foreach($result as $key=>$value) {
				$this->$key = $value;
			}
		}
	}
	/**
	 * Updates the survey class
	 */
	public function updateSurvey($data){
		$sql = mysql_query("UPDATE `survey` SET
			`survey_name` = '$data->survey_name',
			`survey_title` = '$data->survey_title',
			`welcome_message` = '$data->welcome_message',
			`confirmation_message` = '$data->confirmation_message',
			`email_subject` = '$data->email_subject',
			`email_message`= '$data->email_message',
			`last_modified`= '$data->last_modified'
			WHERE `id`='$this->id'") or die(mysql_error());
		return true;
	}
	/**
	 * Returns all questions belonging to this survey
	 */
	public function getAllQuestions() {
		$query = mysql_query("SELECT * FROM `question` WHERE `survey_id` = '$this->id' ORDER BY `id`") or die(mysql_error());
		while($r = mysql_fetch_object($query)) {
			$questions[] = new Question($r->id);
		}
		if(isset($questions)) {
			return $questions;
		} else {
			return false;
		}
	}
	/**
	 * Returns a single question created for this survey
	 * @param $id requires the question id
	 */
	public function getQuestion($id) {
		$query = mysql_query("SELECT * FROM `question` WHERE  `id` = $id") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			return new Question($result->id);
		} else {
			return false;
		}
	}
	/**
	 * Adds a new question to this survey
	 * @param $data data string required
	 */
	public function addQuestion($data) {
		$query = mysql_query("INSERT INTO `question` VALUES (
			NULL,
			'$this->id',
			'$data->type',
			'$data->required',
			'$data->question',
			'$data->create_section',
			'$data->order',
			'$data->response1',
			'$data->response2',
			'$data->response3',
			'$data->response4',
			'$data->response5',
			'$data->response6')")
		or die(mysql_error());
	}
	/**
	 * Deletes a question from this survey
	 * @param $id requires the question id
	 */
	public function deleteQuestion($id) {
		$sql = mysql_query("DELETE FROM `question` WHERE  `id` = '$id'") or die(mysql_error());
		return true;
	}
	/**
	 * Returns the total number of responses made to this survey
	 */
	public function getTotalNumberOfResponses() {
		$sql = "SELECT * FROM `response` WHERE `survey_id` = '$this->id'";
		$result = mysql_query($sql) or die("getting number of responses".mysql_error());
		$total_response = mysql_num_rows($result);
		if($total_response) {
			return $total_response;
		} else {
			return false;
		}
	}
	/**
	 * Returns all conditional queries made for questions belonging to this survey
	 */
	public function getAllConditions() {
		$sql = "SELECT * FROM `branching` WHERE `survey_id` = '$this->id'";
		$result = mysql_query($sql)or die("Check Condition:".mysql_error());
		$i = 0;
		while($c = mysql_fetch_object($result)) {
			$condition[$i]['id'] = $c->id;
			$condition[$i]['question_1_id'] = $c->question_1_id;
			$condition[$i]['question_2_id'] = $c->question_2_id;
			$condition[$i]['condition']=$c->condition;
			$condition[$i]['condition_value']= $c->value;
			$i++;
		}
		if($condition) {
			return $condition;
		} else {
			return false;
		}
	}
}
?>